package com.qianhui.common;

import com.qianhui.dao.*;
import com.qianhui.pojo.*;
import weixin.popular.api.MessageAPI;
import weixin.popular.bean.message.templatemessage.TemplateMessage;
import weixin.popular.bean.message.templatemessage.TemplateMessageItem;
import weixin.popular.support.TokenManager;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * 推送微信通知
 *
 * @author Admin
 */
public class CP
{
    /**
     * 提醒买家付款
     *
     * @param toUser
     * @param order
     */
    public static void push_order_not_pay_info(String toUser, Order order)
    {
        if (CM.validIsEmpty(toUser) == false && order != null)
        {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分");

            TemplateMessage templateMessage = new TemplateMessage();
            templateMessage.setTouser(toUser);
            templateMessage.setTemplate_id("FgWEnTlnzRs5bJiKvArFPLpDeiuujM3lOUCPtSMzpCM");
            templateMessage.setUrl(GlobalConfig.base_path + "go_to_page?go_to_page=" + GlobalConfig.base_path + "show_order_detail?id=" + order.getOrderId());
            templateMessage.setTopcolor("#173177");

            LinkedHashMap<String, TemplateMessageItem> messageMap = new LinkedHashMap<String, TemplateMessageItem>();
            messageMap.put("frist", new TemplateMessageItem("您还有未付款的订单，1小时后若还未付款将会被取消，请尽快支付！\r\n", "#173177"));
            messageMap.put("orderProductPrice", new TemplateMessageItem(order.getOrderTotalPrice() + "元", "#173177"));
            messageMap.put("orderProductName", new TemplateMessageItem(order.getOrderName(), "#173177"));
            messageMap.put("orderAddress", new TemplateMessageItem(order.getOrderBuyerName() + "  " + order.getOrderBuyerAddress(), "#173177"));
            messageMap.put("orderName", new TemplateMessageItem(order.getOrderId(), "#173177"));
            messageMap.put("remark", new TemplateMessageItem("\r\n点击查看详情", "#173177"));

            templateMessage.setData(messageMap);

            MessageAPI.messageTemplateSend(TokenManager.getToken(GlobalConfig.appID), templateMessage);
        }
    }

    /**
     * 提醒买家订单已经被关闭
     *
     * @param toUser
     * @param order
     */
    public static void push_order_have_been_cancelled(String toUser, Order order)
    {
        BuyerDAO buyerDAO = new BuyerDAO();

        if (CM.validIsEmpty(toUser) == false && order != null)
        {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分");

            TemplateMessage templateMessage = new TemplateMessage();
            templateMessage.setTouser(toUser);
            templateMessage.setTemplate_id("WeHkwHZxpEOHPDgpyZLwf1fcuUXOhtLvXwUo4sgUzaQ");
            templateMessage.setUrl(GlobalConfig.base_path + "go_to_page?go_to_page=" + GlobalConfig.base_path + "show_order_detail?id=" + order.getOrderId());
            templateMessage.setTopcolor("#173177");

            LinkedHashMap<String, TemplateMessageItem> messageMap = new LinkedHashMap<String, TemplateMessageItem>();
            messageMap.put("first", new TemplateMessageItem("您的订单由于超时未付款，已被系统取消订单。\r\n", "#173177"));
            messageMap.put("orderProductPrice", new TemplateMessageItem(order.getOrderTotalPrice() + "元", "#173177"));
            messageMap.put("orderProductName", new TemplateMessageItem(order.getOrderName(), "#173177"));
            messageMap.put("orderAddress", new TemplateMessageItem(order.getOrderBuyerName() + "  " + order.getOrderBuyerAddress(), "#173177"));
            messageMap.put("orderName", new TemplateMessageItem(order.getOrderId(), "#173177"));
            messageMap.put("remark", new TemplateMessageItem("\r\n点击查看详情，如有疑问可直接回复此公众号联系客服。", "#173177"));

            templateMessage.setData(messageMap);

            MessageAPI.messageTemplateSend(TokenManager.getToken(GlobalConfig.appID), templateMessage);
        }

        Buyer buyer = buyerDAO.findByBuyerWeChatId(toUser);
        if (buyer != null && CM.validIsEmptyWithTrim(buyer.getBuyerIntroducer()) == false)
        {
            // 查看介绍人是否绑定了微信
            Buyer buyerIntroducer = buyerDAO.findById(buyer.getBuyerIntroducer());
            if (buyerIntroducer == null || CM.validIsEmptyWithTrim(buyerIntroducer.getBuyerWechat()))
            {
                return;
            }
            {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分");

                // 一级上线
                TemplateMessage templateMessage = new TemplateMessage();
                templateMessage.setTouser(buyerIntroducer.getBuyerWechat());
                templateMessage.setTemplate_id("WeHkwHZxpEOHPDgpyZLwf1fcuUXOhtLvXwUo4sgUzaQ");
                templateMessage.setTopcolor("#173177");

                LinkedHashMap<String, TemplateMessageItem> messageMap = new LinkedHashMap<String, TemplateMessageItem>();
                messageMap.put("first", new TemplateMessageItem("您的下线'" + buyer.getBuyerNickname() + "'的订单由于超时未付款，已被系统取消订单。\r\n", "#173177"));
                messageMap.put("orderProductPrice", new TemplateMessageItem(order.getOrderTotalPrice() + "元", "#173177"));
                messageMap.put("orderProductName", new TemplateMessageItem(order.getOrderName(), "#173177"));
                messageMap.put("orderAddress", new TemplateMessageItem(order.getOrderBuyerName() + "  " + order.getOrderBuyerAddress(), "#173177"));
                messageMap.put("orderName", new TemplateMessageItem(order.getOrderId(), "#173177"));
                messageMap.put("remark", new TemplateMessageItem("\r\n下线取消订单后，您将不能获得佣金。", "#173177"));

                templateMessage.setData(messageMap);

                MessageAPI.messageTemplateSend(TokenManager.getToken(GlobalConfig.appID), templateMessage);
            }
            // 二级上线
            if (CM.validIsEmptyWithTrim(buyerIntroducer.getBuyerIntroducer()) == false)
            {
                Buyer buyerIntroducer_2 = buyerDAO.findById(buyerIntroducer.getBuyerIntroducer());
                if (buyerIntroducer_2 == null || CM.validIsEmptyWithTrim(buyerIntroducer_2.getBuyerWechat()))
                {
                    return;
                }

                SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分");

                TemplateMessage templateMessage = new TemplateMessage();
                templateMessage.setTouser(buyerIntroducer_2.getBuyerWechat());
                templateMessage.setTemplate_id("WeHkwHZxpEOHPDgpyZLwf1fcuUXOhtLvXwUo4sgUzaQ");
                templateMessage.setTopcolor("#173177");

                LinkedHashMap<String, TemplateMessageItem> messageMap = new LinkedHashMap<String, TemplateMessageItem>();
                messageMap.put("first", new TemplateMessageItem("您的下线'" + buyerIntroducer.getBuyerNickname() + "'的下线'" + buyer.getBuyerNickname() + "'的订单由于超时未付款，已被系统取消订单。\r\n", "#173177"));
                messageMap.put("orderProductPrice", new TemplateMessageItem(order.getOrderTotalPrice() + "元", "#173177"));
                messageMap.put("orderProductName", new TemplateMessageItem(order.getOrderName(), "#173177"));
                messageMap.put("orderAddress", new TemplateMessageItem(order.getOrderBuyerName() + "  " + order.getOrderBuyerAddress(), "#173177"));
                messageMap.put("orderName", new TemplateMessageItem(order.getOrderId(), "#173177"));
                messageMap.put("remark", new TemplateMessageItem("\r\n下线取消订单后，您将不能获得佣金。", "#173177"));

                templateMessage.setData(messageMap);

                MessageAPI.messageTemplateSend(TokenManager.getToken(GlobalConfig.appID), templateMessage);
            }
        }
    }

    /**
     * 成功付款通知
     *
     * @param toUser
     * @param order
     */
    public static void push_pay_money_success(String toUser, Order order)
    {
        if (CM.validIsEmpty(toUser) == false && order != null)
        {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分");

            TemplateMessage templateMessage = new TemplateMessage();
            templateMessage.setTouser(toUser);
            templateMessage.setTemplate_id("SbyadnOmDN9chDeMBxx2LGQaJU_zffNF1Zo6noZxkwY");
            templateMessage.setUrl(GlobalConfig.base_path + "go_to_page?go_to_page=" + GlobalConfig.base_path + "show_order_detail?id=" + order.getOrderId());
            templateMessage.setTopcolor("#173177");

            LinkedHashMap<String, TemplateMessageItem> messageMap = new LinkedHashMap<String, TemplateMessageItem>();
            messageMap.put("first", new TemplateMessageItem("您好，您的商品已支付成功。\r\n", "#173177"));
            messageMap.put("keyword1", new TemplateMessageItem(order.getOrderId(), "#173177"));
            messageMap.put("keyword2", new TemplateMessageItem(sdf.format(order.getOrderBuyTime()), "#173177"));
            messageMap.put("keyword3", new TemplateMessageItem(String.valueOf(order.getOrderTotalPrice()) + "元", "#173177"));
            messageMap.put("remark", new TemplateMessageItem("\r\n点击查看详情", "#173177"));

            templateMessage.setData(messageMap);

            MessageAPI.messageTemplateSend(TokenManager.getToken(GlobalConfig.appID), templateMessage);
        }
    }

    /**
     * 订单发货通知
     *
     * @param toUser
     * @param order
     */
    public static void push_send_out_order_success(String toUser, Order order)
    {
        if (CM.validIsEmpty(toUser) == false && order != null)
        {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分");

            TemplateMessage templateMessage = new TemplateMessage();
            templateMessage.setTouser(toUser);
            templateMessage.setTemplate_id("kEsIv37iIipWl4OnOIj2tljUarhT-YoeDbdRWeXk5c8");
            templateMessage.setUrl(GlobalConfig.base_path + "go_to_page?go_to_page=" + GlobalConfig.base_path + "show_order_detail?id=" + order.getOrderId());
            templateMessage.setTopcolor("#173177");

            LinkedHashMap<String, TemplateMessageItem> messageMap = new LinkedHashMap<String, TemplateMessageItem>();
            messageMap.put("first", new TemplateMessageItem("您的订单已发货，我们正加速送到您的手上。\r\n", "#173177"));
            messageMap.put("keyword1", new TemplateMessageItem(order.getOrderName(), "#173177"));
            messageMap.put("keyword2", new TemplateMessageItem(order.getOrderExpressCompany(), "#173177"));
            messageMap.put("keyword3", new TemplateMessageItem(order.getOrderExpressNumber(), "#173177"));
            messageMap.put("keyword4", new TemplateMessageItem(order.getOrderBuyerName() + "  " + order.getOrderBuyerAddress(), "#173177"));
            messageMap.put("remark", new TemplateMessageItem("\r\n请您耐心等候。", "#173177"));

            templateMessage.setData(messageMap);

            MessageAPI.messageTemplateSend(TokenManager.getToken(GlobalConfig.appID), templateMessage);
        }
    }

    /**
     * 退货申请提醒
     *
     * @param toUserList
     * @param order
     * @param orderReturn
     */
    public static void push_order_return_apply_info(Order order, OrderReturn orderReturn)
    {
        if (order != null && orderReturn != null)
        {
            KefuDAO kefuDAO = new KefuDAO();
            BuyerDAO buyerDAO = new BuyerDAO();
            List<Kefu> kefuList = kefuDAO.findAll();
            if (kefuList == null)
            {
                return;
            }
            List<String> buyerIdList = new ArrayList<String>();
            for (Kefu kefu : kefuList)
            {
                buyerIdList.add(kefu.getBuyerId());
            }
            List<Buyer> buyerList = buyerDAO.findByIdList(buyerIdList);
            List<String> toUserList = new ArrayList<String>();
            for (Buyer buyer : buyerList)
            {
                toUserList.add(buyer.getBuyerWechat());
            }

            for (String toUser : toUserList)
            {
                if (CM.validIsEmpty(toUser) == false)
                {
                    System.out.println("给" + toUser + "发送退换货信息");
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分");

                    TemplateMessage templateMessage = new TemplateMessage();
                    templateMessage.setTouser(toUser);
                    templateMessage.setTemplate_id("FNQSASABihcQA4jG4NLiaqJY405Bm80YHBRdoxxpqnA");
                    templateMessage.setTopcolor("#173177");

                    LinkedHashMap<String, TemplateMessageItem> messageMap = new LinkedHashMap<String, TemplateMessageItem>();
                    messageMap.put("first", new TemplateMessageItem("有买家申请" + orderReturn.getOrderReturnType() + "，请登录后台管理系统进行处理。\r\n", "#173177"));
                    messageMap.put("keyword1", new TemplateMessageItem(order.getOrderId(), "#173177"));
                    messageMap.put("keyword2", new TemplateMessageItem(order.getOrderName(), "#173177"));
                    messageMap.put("keyword3", new TemplateMessageItem(order.getOrderTotalPrice() + "元", "#173177"));
                    messageMap.put("remark", new TemplateMessageItem("\r\n" + orderReturn.getOrderReturnType() + "理由：" + orderReturn.getOrderReturnReason(), "#173177"));

                    templateMessage.setData(messageMap);

                    MessageAPI.messageTemplateSend(TokenManager.getToken(GlobalConfig.appID), templateMessage);
                }
            }
        }
    }

    /**
     * 退换货审核通知
     *
     * @param toUser
     * @param order
     * @param orderReturn
     */
    public static void push_order_return_auth_result(String toUser, Order order, OrderReturn orderReturn)
    {
        if (CM.validIsEmpty(toUser) == false && order != null && orderReturn != null)
        {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分");

            TemplateMessage templateMessage = new TemplateMessage();
            templateMessage.setTouser(toUser);
            templateMessage.setTemplate_id("ZkIdFCCEbGOaBMbaGO-nVKxVUecatV5bSEqmn9pSgMU");
            templateMessage.setUrl(GlobalConfig.base_path + "go_to_page?go_to_page=" + GlobalConfig.base_path + "show_order_return_list");
            templateMessage.setTopcolor("#173177");

            LinkedHashMap<String, TemplateMessageItem> messageMap = new LinkedHashMap<String, TemplateMessageItem>();
            if (orderReturn.getOrderReturnStatus().equals(IStatus.ORDER_RETURN_BUYER_RETURNING))
            {
                messageMap.put("first", new TemplateMessageItem("您好，您的" + orderReturn.getOrderReturnType() + "申请已经获得通过，您可以将商品寄回给卖家。\r\n", "#173177"));
            }
            else if (orderReturn.getOrderReturnStatus().equals(IStatus.ORDER_RETURN_APPLY_REFUSED))
            {
                messageMap.put("first", new TemplateMessageItem("您好，您的" + orderReturn.getOrderReturnType() + "申请被卖家拒绝了。\r\n", "#173177"));
            }
            if (orderReturn.getOrderReturnStatus().equals(IStatus.ORDER_RETURN_BUYER_RETURNING))
            {
                messageMap.put("keyword1", new TemplateMessageItem("审核通过", "#173177"));
                messageMap.put("keyword4", new TemplateMessageItem("审核通过", "#173177"));
            }
            else if (orderReturn.getOrderReturnStatus().equals(IStatus.ORDER_RETURN_APPLY_REFUSED))
            {
                messageMap.put("keyword1", new TemplateMessageItem("审核未通过", "#173177"));
                messageMap.put("keyword4", new TemplateMessageItem("审核未通过", "#173177"));
            }

            messageMap.put("keyword2", new TemplateMessageItem(order.getOrderName(), "#173177"));
            messageMap.put("keyword3", new TemplateMessageItem(String.valueOf(order.getOrderTotalPrice()) + "元", "#173177"));

            messageMap.put("keyword5", new TemplateMessageItem(sdf.format(Calendar.getInstance().getTime()), "#173177"));
            messageMap.put("remark", new TemplateMessageItem("\r\n感谢您的支持。", "#173177"));

            templateMessage.setData(messageMap);

            MessageAPI.messageTemplateSend(TokenManager.getToken(GlobalConfig.appID), templateMessage);
        }
    }

    /**
     * 换货卖家发货信息
     *
     * @param toUser
     * @param order
     * @param orderReturn
     */
    public static void push_order_return_seller_express_info(String toUser, Order order, OrderReturn orderReturn)
    {
        if (CM.validIsEmpty(toUser) == false && order != null && orderReturn != null)
        {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分");

            TemplateMessage templateMessage = new TemplateMessage();
            templateMessage.setTouser(toUser);
            templateMessage.setTemplate_id("kEsIv37iIipWl4OnOIj2tljUarhT-YoeDbdRWeXk5c8");
            templateMessage.setUrl(GlobalConfig.base_path + "go_to_page?go_to_page=" + GlobalConfig.base_path + "show_order_return_list");
            templateMessage.setTopcolor("#173177");

            LinkedHashMap<String, TemplateMessageItem> messageMap = new LinkedHashMap<String, TemplateMessageItem>();
            messageMap.put("first", new TemplateMessageItem("您的" + orderReturn.getOrderReturnType() + "订单已发货，我们正加速送到您的手上。\r\n", "#173177"));
            messageMap.put("keyword1", new TemplateMessageItem(order.getOrderName(), "#173177"));
            messageMap.put("keyword2", new TemplateMessageItem(orderReturn.getOrderSellerExpressCompany(), "#173177"));
            messageMap.put("keyword3", new TemplateMessageItem(orderReturn.getOrderSellerExpressNumber(), "#173177"));
            messageMap.put("keyword4", new TemplateMessageItem(order.getOrderBuyerName() + "  " + order.getOrderBuyerAddress(), "#173177"));
            messageMap.put("remark", new TemplateMessageItem("\r\n请您耐心等候。", "#173177"));

            templateMessage.setData(messageMap);

            MessageAPI.messageTemplateSend(TokenManager.getToken(GlobalConfig.appID), templateMessage);
        }
    }

    /**
     * 退货卖家退钱给买家
     *
     * @param toUser
     * @param order
     * @param orderReturn
     */
    public static void push_order_return_money_info(String toUser, Order order, OrderReturn orderReturn)
    {
        if (CM.validIsEmpty(toUser) == false && order != null && orderReturn != null)
        {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分");

            TemplateMessage templateMessage = new TemplateMessage();
            templateMessage.setTouser(toUser);
            templateMessage.setTemplate_id("qcYz1FzcMM6R_X9vcezjEtYpsAiiJ4jTs7b8ShR0_Uw");
            templateMessage.setUrl(GlobalConfig.base_path + "go_to_page?go_to_page=" + GlobalConfig.base_path + "show_order_return_list");
            templateMessage.setTopcolor("#173177");

            LinkedHashMap<String, TemplateMessageItem> messageMap = new LinkedHashMap<String, TemplateMessageItem>();
            messageMap.put("first", new TemplateMessageItem("您的" + orderReturn.getOrderReturnType() + "订单已经完成退款，请留意查收。\r\n", "#173177"));
            messageMap.put("orderProductPrice", new TemplateMessageItem("协商金额", "#173177"));
            messageMap.put("orderProductName", new TemplateMessageItem(order.getOrderName(), "#173177"));
            messageMap.put("orderName", new TemplateMessageItem(order.getOrderId(), "#173177"));
            messageMap.put("remark", new TemplateMessageItem("\r\n感谢您的支持。", "#173177"));

            templateMessage.setData(messageMap);

            MessageAPI.messageTemplateSend(TokenManager.getToken(GlobalConfig.appID), templateMessage);
        }
    }

    /**
     * 订单已完成后发送佣金信息给推荐人
     *
     * @param toUser
     * @param order
     */
    public static void push_order_completed_commission_info(Buyer buyer, Order order)
    {

        GoodsDAO goodsDAO = new GoodsDAO();
        OrderGoodsDAO orderGoodsDAO = new OrderGoodsDAO();
        CommentDAO commentDAO = new CommentDAO();
        BuyerDAO buyerDAO = new BuyerDAO();
        OrderDAO orderDAO = new OrderDAO();
        GoodsCommissionDAO goodsCommissionDAO = new GoodsCommissionDAO();

        if (buyer != null && CM.validIsEmptyWithTrim(buyer.getBuyerIntroducer()) == false && order != null)
        {
            // 查看介绍人是否绑定了微信
            Buyer buyerIntroducer = buyerDAO.findById(buyer.getBuyerIntroducer());
            if (buyerIntroducer == null || CM.validIsEmptyWithTrim(buyerIntroducer.getBuyerWechat()))
            {
                return;
            }
            {
                // 计算这个订单是否有佣金产生给上线
                BigDecimal total_commission = new BigDecimal(0);
                List<OrderGoods> orderGoodsList = orderGoodsDAO.findByOrderId(order.getOrderId());

                String goods_info_str = "\r\n";
                for (OrderGoods orderGoodsTemp : orderGoodsList)
                {
                    Goods goodsTemp = goodsDAO.findById(orderGoodsTemp.getGoodsId());

                    goods_info_str += goodsTemp.getGoodsName() + "(" + orderGoodsTemp.getGoodsNumber() + "件)\r\n";

                    GoodsCommission specialCommission = goodsCommissionDAO.findByGoodsIdAndBuyerId(goodsTemp.getGoodsId(), buyerIntroducer.getBuyerId());
                    if (specialCommission == null)
                    {
                        if (goodsTemp.getGoodsCommission() != null)
                        {
                            total_commission = total_commission.add(new BigDecimal(goodsTemp.getGoodsCommission()).multiply(new BigDecimal(orderGoodsTemp.getGoodsNumber())));
                        }
                    }
                    else
                    {
                        total_commission = total_commission.add(new BigDecimal(specialCommission.getGoodsCommission()).multiply(new BigDecimal(orderGoodsTemp.getGoodsNumber())));
                    }
                }

                if (total_commission.doubleValue() <= 0)
                {
                    return;
                }

                String commission_path = "我→";
                commission_path += CM.validIsEmptyWithTrim(buyer.getBuyerNickname()) ? "下线" : buyer.getBuyerNickname();

                SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分");

                // 一级上线
                TemplateMessage templateMessage = new TemplateMessage();
                templateMessage.setTouser(buyerIntroducer.getBuyerWechat());
                templateMessage.setTemplate_id("Bzl8TtIeXyjJ6utK1juVy5UsPi_TwzKse0eM7e0gx2U");
                templateMessage.setTopcolor("#173177");

                LinkedHashMap<String, TemplateMessageItem> messageMap = new LinkedHashMap<String, TemplateMessageItem>();
                messageMap.put("first", new TemplateMessageItem("有推荐的买家成功付款，预计您将会获得一笔新的佣金。\r\n佣金路径：" + commission_path + "\r\n", "#173177"));

                messageMap.put("keyword1", new TemplateMessageItem(total_commission.doubleValue() + "元", "#173177"));
                messageMap.put("keyword2", new TemplateMessageItem(sdf.format(Calendar.getInstance().getTime()), "#173177"));

                messageMap.put("remark", new TemplateMessageItem("\r\n订单号：" + order.getOrderId() + "\r\n总金额：" + order.getOrderTotalPrice() + goods_info_str + "\r\n推荐的买家收货评价后，您将会获得这笔佣金。这里给出预测值，以个人中心查到的数值为准。", "#173177"));

                templateMessage.setData(messageMap);

                MessageAPI.messageTemplateSend(TokenManager.getToken(GlobalConfig.appID), templateMessage);
            }
            // 二级上线
            if (CM.validIsEmptyWithTrim(buyerIntroducer.getBuyerIntroducer()) == false)
            {
                Buyer buyerIntroducer_2 = buyerDAO.findById(buyerIntroducer.getBuyerIntroducer());
                if (buyerIntroducer_2 == null || CM.validIsEmptyWithTrim(buyerIntroducer_2.getBuyerWechat()))
                {
                    return;
                }

                // 计算这个订单是否有佣金产生给上线
                BigDecimal total_commission = new BigDecimal(0);
                List<OrderGoods> orderGoodsList = orderGoodsDAO.findByOrderId(order.getOrderId());

                String goods_info_str = "\r\n";
                for (OrderGoods orderGoodsTemp : orderGoodsList)
                {
                    Goods goodsTemp = goodsDAO.findById(orderGoodsTemp.getGoodsId());

                    goods_info_str += goodsTemp.getGoodsName() + "(" + orderGoodsTemp.getGoodsNumber() + "件)\r\n";

                    //二级上线
                    GoodsCommission specialCommission = goodsCommissionDAO.findByGoodsIdAndBuyerId(goodsTemp.getGoodsId(), buyerIntroducer_2.getBuyerId());

                    // 查看此商品对此一级上线人员是否有特殊佣金
                    GoodsCommission firstLevelShangXian_specialCommission = goodsCommissionDAO.findByGoodsIdAndBuyerId(goodsTemp.getGoodsId(), buyerIntroducer.getBuyerId());
                    double firstLevelShangXian_commission_number = firstLevelShangXian_specialCommission == null ? (goodsTemp.getGoodsCommission() == null ? 0 : goodsTemp.getGoodsCommission()) : firstLevelShangXian_specialCommission.getGoodsCommission();

                    if (specialCommission == null)
                    {
                        if (goodsTemp.getGoodsCommission() != null)
                        {
                            total_commission = total_commission.add(new BigDecimal(goodsTemp.getGoodsCommission() - firstLevelShangXian_commission_number).multiply(new BigDecimal(orderGoodsTemp.getGoodsNumber())));
                        }
                    }
                    else
                    {
                        total_commission = total_commission.add(new BigDecimal(specialCommission.getGoodsCommission() - firstLevelShangXian_commission_number).multiply(new BigDecimal(orderGoodsTemp.getGoodsNumber())));
                    }
                }

                if (total_commission.doubleValue() <= 0)
                {
                    return;
                }

                String commission_path = "我→";
                commission_path += (CM.validIsEmptyWithTrim(buyerIntroducer.getBuyerNickname()) ? "下线" : buyerIntroducer.getBuyerNickname()) + "→";
                commission_path += CM.validIsEmptyWithTrim(buyer.getBuyerNickname()) ? "下线" : buyer.getBuyerNickname();

                SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分");

                TemplateMessage templateMessage = new TemplateMessage();
                templateMessage.setTouser(buyerIntroducer_2.getBuyerWechat());
                templateMessage.setTemplate_id("Bzl8TtIeXyjJ6utK1juVy5UsPi_TwzKse0eM7e0gx2U");
                templateMessage.setTopcolor("#173177");

                LinkedHashMap<String, TemplateMessageItem> messageMap = new LinkedHashMap<String, TemplateMessageItem>();
                messageMap.put("first", new TemplateMessageItem("有推荐的买家成功付款，预计您将会获得一笔新的佣金。\r\n佣金路径：" + commission_path + "\r\n", "#173177"));

                messageMap.put("keyword1", new TemplateMessageItem(total_commission.doubleValue() + "元", "#173177"));
                messageMap.put("keyword2", new TemplateMessageItem(sdf.format(Calendar.getInstance().getTime()), "#173177"));
                messageMap.put("remark", new TemplateMessageItem("\r\n订单号：" + order.getOrderId() + "\r\n总金额：" + order.getOrderTotalPrice() + goods_info_str + "\r\n推荐的买家收货评价后，您将会获得这笔佣金。这里给出预测值，以个人中心查到的数值为准。", "#173177"));

                templateMessage.setData(messageMap);

                MessageAPI.messageTemplateSend(TokenManager.getToken(GlobalConfig.appID), templateMessage);
            }
        }
    }

    /**
     * 新用户关注公众号后推送信息给上线
     *
     * @param buyer
     */
    public static void push_new_user_info(Buyer buyer)
    {
        BuyerDAO buyerDAO = new BuyerDAO();

        if (buyer != null && CM.validIsEmptyWithTrim(buyer.getBuyerIntroducer()) == false)
        {
            // 查看介绍人是否绑定了微信
            Buyer buyerIntroducer = buyerDAO.findById(buyer.getBuyerIntroducer());
            if (buyerIntroducer == null || CM.validIsEmptyWithTrim(buyerIntroducer.getBuyerWechat()))
            {
                return;
            }
            {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分");

                // 一级上线
                TemplateMessage templateMessage = new TemplateMessage();
                templateMessage.setTouser(buyerIntroducer.getBuyerWechat());
                templateMessage.setTemplate_id("rlDhuFqB4o_2yXcb4Z79d4PciOScn-WK_LQgE2eV7Zc");
                templateMessage.setTopcolor("#173177");

                LinkedHashMap<String, TemplateMessageItem> messageMap = new LinkedHashMap<String, TemplateMessageItem>();
                messageMap.put("first", new TemplateMessageItem("您好，以下用户是通过您的推荐(二维码)关注我们的" + "\r\n", "#173177"));

                messageMap.put("keyword1", new TemplateMessageItem(CM.validIsEmpty(buyer.getBuyerNickname()) ? "匿名" : buyer.getBuyerNickname(), "#173177"));
                messageMap.put("keyword2", new TemplateMessageItem(sdf.format(Calendar.getInstance().getTime()), "#173177"));
                messageMap.put("keyword3", new TemplateMessageItem("您", "#173177"));
                messageMap.put("remark", new TemplateMessageItem("\r\n推荐的买家购物后，您将会获得佣金。", "#173177"));

                templateMessage.setData(messageMap);

                MessageAPI.messageTemplateSend(TokenManager.getToken(GlobalConfig.appID), templateMessage);
            }
            // 二级上线
            if (CM.validIsEmptyWithTrim(buyerIntroducer.getBuyerIntroducer()) == false)
            {
                Buyer buyerIntroducer_2 = buyerDAO.findById(buyerIntroducer.getBuyerIntroducer());
                if (buyerIntroducer_2 == null || CM.validIsEmptyWithTrim(buyerIntroducer_2.getBuyerWechat()))
                {
                    return;
                }

                SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分");

                TemplateMessage templateMessage = new TemplateMessage();
                templateMessage.setTouser(buyerIntroducer_2.getBuyerWechat());
                templateMessage.setTemplate_id("rlDhuFqB4o_2yXcb4Z79d4PciOScn-WK_LQgE2eV7Zc");
                templateMessage.setTopcolor("#173177");

                LinkedHashMap<String, TemplateMessageItem> messageMap = new LinkedHashMap<String, TemplateMessageItem>();
                messageMap.put("first", new TemplateMessageItem("您好，以下用户是通过您的推荐(下线的二维码)关注我们的" + "\r\n", "#173177"));

                messageMap.put("keyword1", new TemplateMessageItem(CM.validIsEmpty(buyer.getBuyerNickname()) ? "匿名" : buyer.getBuyerNickname(), "#173177"));
                messageMap.put("keyword2", new TemplateMessageItem(sdf.format(Calendar.getInstance().getTime()), "#173177"));
                messageMap.put("keyword3", new TemplateMessageItem(CM.validIsEmpty(buyerIntroducer.getBuyerNickname()) ? "" : buyerIntroducer.getBuyerNickname(), "#173177"));
                messageMap.put("remark", new TemplateMessageItem("\r\n推荐的买家购物后，您将会获得佣金。", "#173177"));

                templateMessage.setData(messageMap);

                MessageAPI.messageTemplateSend(TokenManager.getToken(GlobalConfig.appID), templateMessage);
            }
        }
    }
}
